package com.kye.express.business.jd_reservation.list;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemJdReservationBinding;
import com.kye.express.view.reclyerview.OnItemClickListener;
import com.kye.model.bean.jd_reservation.JDReservationBean;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

public class JDReservationViewBinder extends ItemViewBinder<JDReservationBean.JDReservation, JDReservationViewBinder.ViewHolder> {
    private OnItemClickListener<JDReservationBean.JDReservation> mOnItemClickListener;
    private OnItemClickListener<JDReservationBean.JDReservation> mOnItemEditClickListener;
    private OnItemClickListener<JDReservationBean.JDReservation> mOnItemAddAgainClickListener;

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_jd_reservation, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JDReservationBean.JDReservation jDReservation) {
        holder.bind(jDReservation);
    }

    @Override
    protected long getItemId(@NonNull JDReservationBean.JDReservation item) {
        return item.getAppointCode().hashCode();
    }

    public OnItemClickListener<JDReservationBean.JDReservation> getOnItemEditClickListener() {
        return mOnItemEditClickListener;
    }

    public void setOnItemEditClickListener(OnItemClickListener<JDReservationBean.JDReservation> onItemEditClickListener) {
        mOnItemEditClickListener = onItemEditClickListener;
    }

    public OnItemClickListener<JDReservationBean.JDReservation> getOnItemAddAgainClickListener() {
        return mOnItemAddAgainClickListener;
    }

    public void setOnItemAddAgainClickListener(OnItemClickListener<JDReservationBean.JDReservation> onItemAddAgainClickListener) {
        mOnItemAddAgainClickListener = onItemAddAgainClickListener;
    }

    public OnItemClickListener<JDReservationBean.JDReservation> getOnItemClickListener() {
        return mOnItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener<JDReservationBean.JDReservation> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemJdReservationBinding mBind;

        ViewHolder(View itemView) {
            super(itemView);
            mBind = ItemJdReservationBinding.bind(itemView);

            RxView.clicks(mBind.getRoot())
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (mOnItemClickListener == null) return;
                            mOnItemClickListener.onClickItem(mBind.getBean(), mBind.getRoot());
                        }
                    });

            RxView.clicks(mBind.buttonEdit)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (mOnItemEditClickListener == null) return;
                            mOnItemEditClickListener.onClickItem(mBind.getBean(), mBind.buttonEdit);
                        }
                    });

            RxView.clicks(mBind.buttonAddAgain)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (mOnItemAddAgainClickListener == null) return;
                            mOnItemAddAgainClickListener.onClickItem(mBind.getBean(), mBind.buttonAddAgain);
                        }
                    });
        }

        public void bind(JDReservationBean.JDReservation jDReservation) {
            mBind.setBean(jDReservation);
        }
    }
}
